package com.apkicon.model;

import java.awt.image.BufferedImage;

/**
 * 图标信息模型
 */
public class IconInfo {
    private BufferedImage image;
    private IconType iconType;
    private int originalWidth;
    private int originalHeight;
    private String sourcePath;

    public IconInfo(BufferedImage image, IconType iconType, String sourcePath) {
        this.image = image;
        this.iconType = iconType;
        this.sourcePath = sourcePath;
        this.originalWidth = image.getWidth();
        this.originalHeight = image.getHeight();
    }

    public BufferedImage getImage() {
        return image;
    }

    public IconType getIconType() {
        return iconType;
    }

    public int getOriginalWidth() {
        return originalWidth;
    }

    public int getOriginalHeight() {
        return originalHeight;
    }

    public String getSourcePath() {
        return sourcePath;
    }

    public void saveAsPng(java.io.File outputFile) throws java.io.IOException {
        // 确保输出目录存在
        java.io.File parentDir = outputFile.getParentFile();
        if (parentDir != null && !parentDir.exists()) {
            parentDir.mkdirs();
        }
        
        // 使用优化的PNG写入器
        javax.imageio.ImageWriter writer = javax.imageio.ImageIO.getImageWritersByFormatName("PNG").next();
        javax.imageio.ImageWriteParam param = writer.getDefaultWriteParam();
        
        // 设置PNG压缩参数 - 大幅减少文件大小
        if (param.canWriteCompressed()) {
            param.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(0.1f); // 极低的质量，大幅减少文件大小
        }
        
        try (javax.imageio.stream.ImageOutputStream output = javax.imageio.ImageIO.createImageOutputStream(outputFile)) {
            writer.setOutput(output);
            writer.write(null, new javax.imageio.IIOImage(image, null, null), param);
        } finally {
            writer.dispose();
        }
    }

    public enum IconType {
        PNG("PNG"),
        WEBP("WebP"),
        VECTOR_DRAWABLE("Vector Drawable"),
        ADAPTIVE_ICON("Adaptive Icon");

        private final String displayName;

        IconType(String displayName) {
            this.displayName = displayName;
        }

        @Override
        public String toString() {
            return displayName;
        }
    }
}
